# _*_coding:utf-8_*_
import time

from ATS.datamanager import DataManager
from ATS.instrumentDriver.InstGenSMU.InstGenSMU_B1500A import SMU_B1500A

strCompare = '+0,"No Error."'


def getIDN(source):
    inst.write(source, "*IDN?")
    result = inst.read(source)
    print("result", result)
    return result


def read_smu_current(source):
    inst.write(source, "FMT 1,1")
    inst.write(source, "CN 2,4")
    inst.write(source, "DV 2,0,0.00001,0.01")  # DV chnum,vrange,voltage[,Icomp[,comp_polarity[,irange]]
    inst.write(source, "DV 4,0,0,0.01")  # DV chnum,vrange,voltage[,Icomp[,comp_polarity[,irange]]
    inst.write(source, "MM 1,2")
    inst.write(source, "XE")
    inst.write(source, "*OPC?")
    result = inst.read(source)
    if result:
        status = result.strip()
        if "1" == status:
            inst.write(source, 'ERRX?')
            time.sleep(1)
            err = str(inst.read(source).strip())
            time.sleep(1)
            print("err", err)
            if strCompare == err:
                data = inst.read(source).strip().split(",")
                print("data", data)
                data = [float(i[3:]) for i in data]
                print("data", data)
                DataManager.get_DM().postarraydata('Frequency', data)
                DataManager.get_DM().postarraydata('I', data)
                DataManager.get_DM().postarraydata('V', data)
